package com.exmaple.lemlin.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.app.Constants;
import com.exmaple.lemlin.entity.ChatRecord;
import com.exmaple.lemlin.entity.ChatRecordDao;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseFragment;
import com.exmaple.lemlin.ui.activity.ChatWindowsActivity;
import com.exmaple.lemlin.ui.activity.MainActivity;
import com.exmaple.lemlin.ui.adapter.ChatAdapter;
import com.exmaple.lemlin.ui.adapter.UserAdapter;
import com.exmaple.lemlin.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainChatFragment extends AbstractBaseFragment implements MainActivity.OnMainChatListener, ChatAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<ChatRecord> records = new ArrayList<>();
    private ChatAdapter chatAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_chat;
    }

    @Override
    protected void initView() {

        records.addAll(APPIM.getDaoSession().getChatRecordDao().queryBuilder()
                .orderDesc(ChatRecordDao.Properties.LastMsgTime).list());
        chatAdapter = new ChatAdapter(getActivity(), records);
        chatAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(chatAdapter);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initLazyLoad() {

    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(getActivity(), ChatWindowsActivity.class);
        intent.putExtra(Constants.BUNDLE_KEY_USER_NAME, records.get(position).getName());
        startActivityForResult(intent, 1);
    }

    @Override
    public void onChatRecord(List<ChatRecord> records) {
        chatAdapter.addData(records);
    }
}
